<?php

namespace common\models\base;

use Yii;

/**
 * This is the model class for table "{{%api_role}}".
 *
 * @property integer $id
 * @property string $name
 * @property integer $created_at
 * @property integer $updated_at
 *
 * @property ApiRoleRoute[] $apiRoleRoutes
 * @property ApiRoute[] $apiRoutes
 * @property ApiRoleUser[] $apiRoleUsers
 * @property User[] $users
 */
class ApiRole extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%api_role}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name', 'created_at', 'updated_at'], 'required'],
            [['created_at', 'updated_at'], 'integer'],
            [['name'], 'string', 'max' => 20],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => 'Name',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getApiRoleRoutes()
    {
        return $this->hasMany(ApiRoleRoute::className(), ['api_role_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getApiRoutes()
    {
        return $this->hasMany(ApiRoute::className(), ['id' => 'api_route_id'])->viaTable('{{%api_role_route}}', ['api_role_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getApiRoleUsers()
    {
        return $this->hasMany(ApiRoleUser::className(), ['api_role_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getUsers()
    {
        return $this->hasMany(User::className(), ['id' => 'user_id'])->viaTable('{{%api_role_user}}', ['api_role_id' => 'id']);
    }
}
